//
//  Test2ViewController.swift
//  JKRegex
//
//  Created by 王冲 on 2018/1/30.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

class Test2ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        view.addSubview(label)
        label.font = UIFont.systemFont(ofSize: 24)
    
        /**
             添加富文本
             1、pattern: 搜索的规则
             2、str: 传进去字符串
             3、font:字体的大小
         */
        
        //let str: String = "<!-- Nav bar -->\n<TD>\n<A HREF=/home><IMG SRC=/imges/home.gif></A>\n<IMG SRC=/images/spacer.gif>\n<A HREF=/search><IMG SRC=/imges/home.gif></A>\n</A>\n<IMG SRC=/images/spacer.gif>\n<A HREF=/help><IMG SRC=/imges/home.gif>\n</TD>"
        //label.attributedText = textRegex(pattern: "(<[Aa]\\s+[^>]+>\\s*)?<[Ii][Mm][Gg]\\s+[^>]+>(?(1)\\s*</[Aa]>)" ,str: str, font: 24)

        //let str: String = "123-456-7890\n(123)456-7890\n(123)-456-7890\n(123-456-7890\n1234567890\n123 456 7890"
        //label.attributedText = textRegex(pattern: "(\\()?\\d{3}(?(1)\\)|-)\\d{3}-\\d{4}" ,str: str, font: 24)
        
        let str: String = "11111\n22222\n33333-\n44444-4444"
        label.attributedText = textRegex(pattern: "\\d{5}(?(?=-)-\\d{4})" ,str: str, font: 24)
    
        
        
     
    }
    

    // 1.匹配纯文本
    func textRegex(pattern: String,str: String,font: CGFloat) -> NSMutableAttributedString{
        
        //富文本设置
        let attributeString = NSMutableAttributedString(string:str)
        do {
            // 1.1.定义规则
            //let pattern = pattern
            // 1.2.创建正则表达式对象
            let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            // 1.3.开始匹配
            let res = regex.matches(in: str, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, str.count))
    
            for checkingRes in res
            {
                print("range\(checkingRes.range)")
                
                // substring 截取符合规定规则的qq号
                print((str as NSString).substring(with: checkingRes.range))
                //从文本checkingRes.range个字符字体HelveticaNeue-Bold
                attributeString.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Bold", size: font)!,range: checkingRes.range)
                //设置字体颜色
                attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue,range: checkingRes.range)
                //设置文字背景颜色
                attributeString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.green,range: checkingRes.range)
            }

            
            return attributeString
            
        } catch {
            
            print(error)
        }
        return attributeString
    }
    
    lazy var label: UILabel = {
        
        let label1: UILabel = UILabel(frame: CGRect(x: 16, y: 0, width: UIScreen.main.bounds.size.width-32, height: UIScreen.main.bounds.size.height))
        label1.numberOfLines = 0
        label1.backgroundColor = UIColor.white
        return label1
    }()
}


